package com.example.springboot.pattern.proxy.cglib;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/3/8
 **/
public class TestCglib {
    public static void main(String[] args) {
//        Enhancer enhancer = new Enhancer();
//        enhancer.setSuperclass(TargetObject.class);
//        enhancer.setCallback(new TargetInterceptor());
//
//        TargetObject targetObject = (TargetObject) enhancer.create();
//
//        System.out.println(targetObject.method1("houry"));

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(TargetObject.class);
        CallbackFilter filter = new TargetMethodCallbackFilter();

        Callback noopCb = NoOp.INSTANCE;
        Callback callback1 = new TargetInterceptor();
        Callback fixed = new TargetResultFixed();

        Callback[] callbacks = new Callback[] {noopCb, callback1, fixed};

        enhancer.setCallbacks(callbacks);
        enhancer.setCallbackFilter(filter);

        TargetObject targetObject = (TargetObject) enhancer.create();


        System.out.println(targetObject.method1("58"));
    }
}
